from flask import Blueprint,render_template,jsonify,redirect,url_for,session
from exts import  mail,db
from flask_mail import Message
from flask import request
import string
import random
from models import EmailCaptchaModel
from .forms import RegisterForm,LoginForm
from models import UserModel
from werkzeug.security import generate_password_hash,check_password_hash

bp = Blueprint("auth", __name__, url_prefix="/auth")

@bp.route("/login",methods=['GET','POST'])
def login():
    if request.method=='GET':
        return render_template("login.html")
    else:
        form=LoginForm(request.form)
        if form.validate():
            email=form.email.data
            password=form.password.data
            user=UserModel.query.filter_by(email=email).first()
            if not user:
                print("邮箱在数据库中不存在！")
                return redirect(url_for("auth.login"))
            if check_password_hash(user.password,password):

                session['user_id']=user.id
                return redirect("/")
            else:
                print("密码错误")
                return redirect(url_for("auth.login"))

        else:
            print(form.errors)
            return redirect(url_for("auth.login"))

#GET从服务器取数据
#POST：将客户端的数据提交给服务器
@bp.route("/register",methods=['GET','POST']) #只能是post或者get，否则报错405 methods are not allowed
def register():
    if request.method=='GET':
        #验证用户提交的邮箱和验证码是否对应且正确
        #表单验证flask-wtf
        return  render_template("register.html")
    else:
        form=RegisterForm(request.form)
        if form.validate():
            email=form.email.data
            username=form.username.data
            password=form.password.data
            user=UserModel(email=email,username=username,password=generate_password_hash(password))
            db.session.add(user)
            db.session.commit()
            return redirect(url_for("auth.login"))
        else:
            print(form.errors)
            return redirect(url_for("auth.register"))

@bp.route("/logout")
def logout():
    session.clear()
    return redirect("/")
   # return redirect(url_for("auth.login"))


#bp.route:如果没有指定methods参数，默认就是GET请求
@bp.route("/captcha/email")
def get_email_captcha():
    email=request.args.get("email")
   #4/6随机字母数字组合
    source=string.digits*4
    captcha=random.sample(source,4)
    captcha="".join(captcha)
    message = Message(subject="论坛注册验证码", recipients=[email], body=f"您的验证码是：{captcha}")
    mail.send(message)
    # memcatched/redis
    # 用数据库表的方式存储
    email_captcha=EmailCaptchaModel(email=email,captcha=captcha)
    db.session.add(email_captcha)
    db.session.commit()
    #RESful API
    #{code:200/400/500,    message:"",data}
    return jsonify({"code":200,"message":"","data":None})


@bp.route("/mail/test")
def mail_test():
     message=Message(subject="邮箱测试",recipients=["hlg18852081105@163.com"],body="这是一条测试邮件")
     mail.send(message)
     return "邮件发送成功！"